<?php
namespace app\api\controller;
use think\Db;
use think\Image;
use think\Request;
use think\Session;
use think\Controller;
class Zhuye extends Controller
{
    public function AjaxReturn($code,$msg,$data=array())
    {
        $returnData=array(
            "code"=>$code
        );
        if($msg)
        {
            $returnData["message"]=$msg;
        }
        if(!empty($data))
        {
            $returnData["data"]=$data;
        }
        exit(json_encode($returnData));
    }
  
	public function rflg()
    {
    	$code=input('openid');
      	$uid=Db::name('user')->where('code',$code)->field('uid')->find();
      	$uid=$uid['uid'];
      	$rflg=Db::name('real')->where('uid',$uid)->field('rflg')->field('tid')->find();
      	return $this->AjaxReturn('1',$rflg);
    }
    //主页面显示(已测试)
    public function index()
    {
      //获取当前域名
        $request = Request::instance();
        $domain=$request->domain();
        $code=input('openid');
        $uid=Db::name('user')->where('code',$code)->field('uid')->find();
        $gid=input('uid');

            $arr = Db::name('user')
                ->join('f_company','f_company.uid=f_user.uid')
                ->join('f_real','f_real.uid=f_user.uid')
                ->join('f_fans','f_fans.uid=f_user.uid')
                ->where('f_user.uid',$gid)
                ->field('f_user.nickname,f_user.avatars_img,f_user.uname,f_user.company,f_user.head_portrait,f_user.phone,f_user.gflg,f_company.*,f_real.rflg,f_fans.*,f_user.position')
                ->find();
            $rss = Db::name('company')->where('uid',$gid)->field('f_company.aid')->find();
          
            //如果该公司的企业能力为空的则为其赋值
            if($rss['aid']==''){
                $rss['aid']='暂无';
            }
            //有值的话则循环显示
            $rs = array_filter(explode(',',$rss['aid']));
            for($i=0;$i<count($rs);$i++)
            {
                $as[] = Db::name('ability')->where('aid',$rs[$i])->field('ability')->select();
            }
            $arr['aid'] = $as;
            $gua=array_filter(explode(',',$arr['fuid']));
            //判断当前登陆用户是否关注查看名片的用户
            if(in_array($uid['uid'],$gua)){
                $arr['guan']='1';
            }else{
                $arr['guan']='0';
            }
          	//查询到粉丝总数
        	$fuid=Db::name('ribiao')->where('bid',$gid)->select();
        	//查询到关注总数
        	$guid = count(array_filter(explode(',',$arr['guid'])));
        	$arr['fuid'] = count($fuid);
        	$arr['guid'] = $guid;
          //查询我的实名状态
          $q=Db::name('real')->where('uid',$uid['uid'])->field('rflg')->field('tid')->find();
          $arr['myrflg']=$q['rflg'];
          $arr['mytid']=$q['tid'];

      	if($arr['rflg'] == 1){
        	$arr['nickname']=$arr['uname'];
        }
        //判断用户头像是否为空为空则显示微信头像，如果两个头像都为空则为用户头像为空的添加一个临时头像（不存在数据库中的临时头像）
        if($gid!='')
        {
            if($arr['avatars_img']=='')
            {
                $rss=Db::name('user')->where('uid',$gid)->field('head_portrait')->find();
                $arr['avatars_img']=$rss['head_portrait'];
                if($arr['avatars_img']=='')
                {
                    $arr['avatars_img']=$domain.'/upload/linshiimg/20190411095739.jpg';
                }
            }else{
                $arr['avatars_img']=$domain.'/upload/portrait/'.$arr['avatars_img'];
            }
        }else{
            if($arr['avatars_img']=='')
            {
                $rss=Db::name('user')->where('uid',$uid['uid'])->field('head_portrait')->find();
                $arr['avatars_img']=$rss['head_portrait'];
                if($arr['avatars_img']=='')
                {
                    $arr['avatars_img']=$domain.'/upload/linshiimg/20190411095739.jpg';
                }
            }else{
                $arr['avatars_img']=$domain.'/upload/portrait/'.$arr['avatars_img'];
            }
        }
        //将四套图片分割成数组
        if($arr['location_img']!='')
        {
            $rc=array_filter(explode(',',$arr['location_img']));
            $rc1=count($rc);
            if($rc1>6)
            {
                for($i=0;$i<6;$i++)
                {
                    $e[]=$domain.'/upload/news/'.$rc[$i];
                    $arr['location_img']=$e;
                }
            }else{
                foreach($rc as $b)
                {
                    $box[]=$domain.'/upload/news/'.$b;
                    $arr['location_img']=$box;
                }
            }
        } else {
            $arr['location_img']=[];
        }
        if($arr['place_img']!='')
        {
            $ra=array_filter(explode(',',$arr['place_img']));
            $ra1=count($ra);
            if($ra1>6)
            {
                for($i=0;$i<6;$i++)
                {
                    $d[]=$domain.'/upload/news/'.$ra[$i];
                    $arr['place_img']=$d;
                }
            }else{
                foreach($ra as $b)
                {
                    $box1[]=$domain.'/upload/news/'.$b;
                    $arr['place_img']=$box1;
                }
            }
        }else{
            $arr['place_img']=[];
        }
        if($arr['equipment_img']!='')
        {
            $rb=array_filter(explode(',',$arr['equipment_img']));
            $rb1=count($rb);
            if($rb1>6)
            {
                for($i=0;$i<6;$i++)
                {
                    $c[]=$domain.'/upload/news/'.$rb[$i];
                    $arr['equipment_img']=$c;
                }
            }else{
                foreach($rb as $b)
                {
                    $box2[]=$domain.'/upload/news/'.$b;
                    $arr['equipment_img']=$box2;
                }
            }
        }else{
            $arr['equipment_img']=[];
        }
        if($arr['license_img']!='')
        {
            $rd=array_filter(explode(',',$arr['license_img']));
            $rd1=count($rd);
            if($rd1>6)
            {
                for($i=0;$i<6;$i++)
                {
                    $a[]=$domain.'/upload/news/'.$rd[$i];
                    $arr['license_img']=$a;
                }
            }else{
                foreach($rd as $b)
                {
                    $box3[]=$domain.'/upload/news/'.$b;
                    $arr['license_img']=$box3;
                }
            }
        }else{
            $arr['license_img']=[];
        }
        if($arr['location_img'] ==[] && $arr['place_img'] == [] && $arr['equipment_img'] == [] && $arr['license_img'] == []){
            $arr['tupian'] = 0;
        }else{
            $arr['tupian']=[$arr['location_img'],$arr['place_img'],$arr['equipment_img'],$arr['license_img']];
        }
        
		$arr[]=Db::name('user')->where('code',$code)->field('phone')->find();
      	
        return $this->AjaxReturn('1',$arr);
    }
    //别人名片产品显示
    public function message()
    {
        $code=input('openid');
        $uid=Db::name('user')->where('code',$code)->field('uid')->find();
        $uid=$uid['uid'];
      
        $gid=input('uid');
		$page=input('page');
      //获取当前域名
        $request = Request::instance();
        $domain=$request->domain();

            $where['uid'] = $gid;
            $where['mflg'] = 1;
            $xinxi = db('message')
                ->where($where)
                ->order('createtime desc')
              	->page($page,7)
                ->select();
     
        if($xinxi==[])
        {
            $xin='';
            return $xin;
        }else{
            for($i=0;$i<count($xinxi);$i++)
            {   
                $xinxi[$i]['myuid'] = $uid;
                $rs[]=array_filter(explode(',',$xinxi[$i]['picture']));
              	$readNum=db('message')->where('mid',$xinxi[$i]['mid'])->value('num');
                $readNum = $readNum + 1;
                $readNum=db('message')->where('mid',$xinxi[$i]['mid'])->update(['num'=>$readNum]);
                if($xinxi[$i]['unit']==1)
                {
                    $xinxi[$i]['unit']='吨';
                }
                elseif($xinxi[$i]['unit']==2)
                {
                    $xinxi[$i]['unit']='公斤';
                }
                elseif($xinxi[$i]['unit']==3)
                {
                    $xinxi[$i]['unit']='斤';
                }elseif($xinxi[$i]['unit']==4)
                {
                    $xinxi[$i]['unit']='个';
                }
            }
            //只输出每组图片的第一个字符串并为其加上地址
            for($i=0;$i<count($rs);$i++)
            {
                if($rs[$i])
                {
                 	//$absolute= "/upload/suo/"; 
                 	//if (!is_dir(ROOT_PATH."/upload/suo/".$rs[$i][0]))
                  //	{
                   	//$image = Image::open(ROOT_PATH."/upload/message/".$rs[$i][0]);
                   	//$pic = $image->thumb(500,500,Image:: THUMB_SCALING)->save(ROOT_PATH.$absolute.$rs[$i][0]);
                	// }
                   // $xinxi[$i]['picture']=$domain.$absolute.$rs[$i][0];
                    $xinxi[$i]['picture']='https://www.agrimanu.cn/upload/message/'.$rs[$i][0];
                }else{
                    $xinxi[$i]['picture']=$domain.'/upload/message/no.jpg';
                }
            }
            return $this->AjaxReturn('1','操作成功',$xinxi);
        }
    }
  
   
   public function InformationList(){
     	$code = input('openid');
     	$phone=Db::name('user')->where('code',$code)->field('phone')->find();
 //获取当前域名
        $request = Request::instance();
        $domain=$request->domain();
            $sql=db('zixun')
                ->where('flg',0)
              	->order('createtime desc')
                ->select();
            foreach ($sql as $key=>$value){
                $value['prctures']=$domain .$value['picture'];
              $value['phone']=$phone['phone'];
                $arr[]=$value;
            }
            if (!empty($sql)){
                return $this->AjaxReturn("-1","该用户未实名认证",$arr);
            }else{
                return $this->AjaxReturn("-2","操作失败");
            }
    }
  /*
     *方法作用：行情资讯详细页面
     * @param $id int 接收过来的文章ID
     *@param $where array 查询条件
     * @return code msg array[]   状态码  提示信息 返回数据
     * */
    public function InformationCheck(){
        $uid=db('user')->where('code',input('openid'))->value('uid');
      	$phone=db('user')->where('uid',$uid)->value('phone');
        $zid=input('id');
        $rs=db('real')
            ->where('uid',$uid)
          	->field('rflg,tid')
            ->find();
        $re=db('user')
            ->where('uid',$uid)
            ->field('gflg')
            ->find();
        if ($rs['rflg']==1||$re['gflg']==1)
        {
            $check=db('zixun')
                ->where('zid',$zid)
                ->find();
            $check['picture']='https://www.agrimanu.cn'. $check['picture'];
            //浏览量加1
            $num=db('zixun')->where('zid',$zid)->field('id')->find();
            $arr8=array_filter(explode(',',$num['id']));
            if(!in_array($uid,$arr8))
            {
                $num1=$num['id'].$uid.',';
                $update=db('zixun')->where('zid',$zid)->update(['id'=>$num1]);
            }
            if (!empty($check))
            {
                return $this->AjaxReturn("1","操作成功",[$check,$rs,$phone]);
            }
            else
            {
                return $this->AjaxReturn("0","操作失败");
            }
        }
        else
        {
            $res=db('zpay')
                ->where('uid',$uid)
                ->where('zid',$zid)
                ->field('flg')
                ->find();
            if($res['flg']=='已支付')
            {
                $check=db('zixun')
                    ->where('zid',$zid)
                    ->find();//dump($check);die;
                $check['picture']='https://www.agrimanu.cn'. $check['picture'];
                $num=db('zixun')->where('zid',$zid)->field('id')->find();
                $arr8=array_filter(explode(',',$num['id']));
                //return $arr8;die;
                if(!in_array($uid,$arr8))
                {
                    $num1=$num['id'].$uid.',';
                    $update=db('zixun')->where('zid',$zid)->update(['id'=>$num1]);
                }
                if (!empty($check))
                {
                    return $this->AjaxReturn("1","操作成功",[$check,$rs,$phone]);
                }
                else
                {
                    return $this->AjaxReturn("0","操作失败");
                }
            }
            else
            {
                $check=db('zixun')
                    ->where('zid',$zid)
                    ->find();
                $check['picture']='https://www.agrimanu.cn'. $check['picture'];
                return $this->AjaxReturn("22","无权查看",[$check,$rs,$phone]);
            }
        }

    }

}